home *** CD-ROM | disk | FTP | other *** search
/ Turnbull China Bikeride / Turnbull China Bikeride - Disc 2.iso / STUTTGART / LANG / C / LIB / UNIXLIB37B / !UnixLib37 / src / stdio / c / fread < prev    next >
Text File  |  1996-11-09  |  2KB  |  70 lines

  1. /****************************************************************************
  2.  *
  3.  * $Source: /unixb/home/unixlib/source/unixlib37/src/stdio/c/RCS/fread,v $
  4.  * $Date: 1996/05/06 09:01:34 $
  5.  * $Revision: 1.2 $
  6.  * $State: Rel $
  7.  * $Author: unixlib $
  8.  *
  9.  * $Log: fread,v $
  10.  * Revision 1.2  1996/05/06 09:01:34  unixlib
  11.  * Updates to sources made by Nick Burrett, Peter Burwood and Simon Callan.
  12.  * Saved for 3.7a release.
  13.  *
  14.  * Revision 1.1  1996/04/19 21:32:42  simon
  15.  * Initial revision
  16.  *
  17.  ***************************************************************************/
  18.  
  19. static const char rcs_id[] = "$Id: fread,v 1.2 1996/05/06 09:01:34 unixlib Rel $";
  20.  
  21. #include <stdlib.h>
  22. #include <string.h>
  23. #include <stdio.h>
  24. #include <unistd.h>
  25.  
  26. __STDIOLIB__
  27.  
  28. int
  29. __fread (register FILE * f, register char *s, int _n)
  30. {
  31.   register int n, i, b, g = f->flag;
  32.  
  33.   if ((g & (_IOREAD | _IOERR | _IOEOF)) != _IOREAD)
  34.     return (-1);
  35.  
  36.   b = (g & _IONBF) ? 1 : f->bufsiz;
  37.  
  38.   n = _n;
  39.  
  40.   while (n)
  41.     {
  42.       if (i = ((n > f->i_cnt) ? f->i_cnt : n))    /* read buffer */
  43.     {
  44.       memcpy (s, f->i_ptr, i);
  45.       f->i_cnt -= i, f->i_ptr += i;
  46.       n -= i, s += i;
  47.     }
  48.       while (n >= b)        /* direct read() */
  49.     {
  50.       if ((i = read (f->fd, s, b)) <= 0)
  51.         {
  52.           f->flag |= ((i) ? _IOERR : _IOEOF);
  53.           i = _n - n;
  54.           return (i ? i : -1);
  55.         }
  56.       f->pos += i, n -= i, s += i;
  57.     }
  58.       if (n)
  59.     {
  60.       if ((i = __filbuf (f)) < 0)    /* fill buffer */
  61.         {
  62.           i = _n - n;
  63.           return (i ? i : -1);
  64.         }
  65.       --n, *s++ = i;
  66.     }
  67.     }
  68.   return (_n - n);
  69. }
  70.